#include <vector>
#include <string>
#include <queue>
#include <map>
#include <set>

//前K个高频单词
using namespace srd;
class Solution {
public:
    class sort
    {
        public:
        bool operator()(const pair<string,int>&p1,const pair<string,int>& p2)
        {
            //要排降序 所以优先级队列的仿函数要排升序
            if(p1.second<p2.second) return true;
            else if(p1.second==p2.second && p1.first>p2.first) return true;
            return false;
        }
    };
    vector<string> topKFrequent(vector<string>& words, int k) 
    {
        map<string,int>m;
        vector<string>res;
        for(auto& e:words)
        {
            m[e]++;
        }
        priority_queue<pair<string,int>,vector<pair<string,int>>,sort>q;
        for(auto e:m)
        {
            q.push(e);
        }
        while(k--)
        {
            res.push_back(q.top().first);
            q.pop();
        }
        return res;
    }
};